﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Taxonomy;
using System.Web.UI.WebControls;

namespace P3.Modules.SharePoint.AutoTag.Layouts.P3.Modules.SharePoint.AutoTag
{
    public partial class AutomaticTaggingConfig : LayoutsPageBase
    {
        SPList list;
        SPWeb web;
        protected void Page_Load(object sender, EventArgs e)
        {
            string listID = Request.QueryString["listID"];
            string siteUrl = Request.QueryString["SiteUrl"];
            SPSite site = SPContext.Current.Site;
            web = site.OpenWeb(siteUrl);
            list = web.Lists[new Guid(listID)];

            string errorMsg;

            foreach (SPField field in list.Fields)
            {
                if ((field.Type == SPFieldType.Note)&&(field.FromBaseType == false)&&(field.Hidden == false)&&(field.Sealed == false))
                    MultiLineDropDown.Items.Add(field.Title);
                else if (field.TypeAsString.Equals("TaxonomyFieldType") || field.TypeAsString.Equals("TaxonomyFieldTypeMulti"))
                    MetadataDropDown.Items.Add(field.Title);
            }

           if ((MultiLineDropDown.Items.Count <= 0) && (MetadataDropDown.Items.Count <=0))
            {
                errorMsg= "No Multiple lines of text field and No Managed metadata field in the list. <br/> Please add one before configuring";
                displayError(errorMsg, 1);
            }
            else if (MultiLineDropDown.Items.Count <= 0)
            {
                errorMsg = "No Multiple lines of text field in the list.<br/>  Please add one before configuring";
                displayError(errorMsg, 2);
            }
           else if (MetadataDropDown.Items.Count <= 0)
           {
               errorMsg = "No Managed metadata field in the list.<br/>  Please add one before configuring";
               displayError(errorMsg, 3);
           }
           else
           {
               string propertyName = "TagConfig_" + list.ID;
               string previousMultilineValue, previousMetadaValue;
               string[] arrPropertyValue;
               if (web.GetProperty(propertyName) != null)
               {
                   arrPropertyValue = (web.GetProperty(propertyName).ToString()).Split('$');
                   previousMultilineValue = arrPropertyValue[0];
                   previousMetadaValue = arrPropertyValue[1];

                   MultiLineDropDown.SelectedValue = previousMultilineValue;
                   MetadataDropDown.SelectedValue = previousMetadaValue;
               }
           }
          
        }
       
        private void displayError(string errorMsg, int errorNumber)
        {
           if(errorNumber == 1)
           {
                MultiLineTextfieldTitle.Visible = false;
                MultiLineTextfieldDesc.Visible = false;
                ManagedMetaDatafieldTitle.Visible = false;
                ManagedMetaDatafieldDesc.Visible = false;
                MultiLineDropDown.Visible = false;
                MetadataDropDown.Visible = false;
            }
           else if (errorNumber == 2)
           {
               MultiLineTextfieldTitle.Visible = false;
               MultiLineTextfieldDesc.Visible = false;                
               MultiLineDropDown.Visible = false;
           }
           else
           {
               ManagedMetaDatafieldTitle.Visible = false;
               ManagedMetaDatafieldDesc.Visible = false;
               MetadataDropDown.Visible = false;
           }
           errorLabel.Text = errorMsg;
           errorLabel.ForeColor = System.Drawing.Color.Red;
           errorLabel.Visible = true;
        }
    }
}
